<script setup>
import pageContainer from '@/components/pageContainer.vue'
import { changeAdminPasswordService } from '@/apis/center'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
const UserStore = useUserStore()
const router = useRouter()
const formRef = ref()
const formModel = ref({
  old_pwd: '',
  new_pwd: '',
  re_pwd: ''
})

// 校验老密码不能与新密码一致
const checkOldPassword = (rule, value, callback) => {
  if (value === formModel.value.old_pwd) {
    callback(new Error('新密码不能与原密码一样'))
  } else {
    callback()
  }
}

// 校验两次输入的新密码必须一致
const checkNewPassword = (rule, value, callback) => {
  if (value !== formModel.value.new_pwd) {
    callback(new Error('两次输入的密码不一致'))
  } else {
    callback()
  }
}

// 校验规则
const rules = {
  old_pwd: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码长度必须是6-15位的非空字符串',
      trigger: 'blur'
    }
  ],
  new_pwd: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码长度必须是6-15位的非空字符串',
      trigger: 'blur'
    },
    {
      validator: checkOldPassword,
      trigger: 'blur'
    }
  ],
  re_pwd: [
    { required: true, message: '请再次输入新密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码长度必须是6-15位的非空字符串',
      trigger: 'blur'
    },
    {
      validator: checkNewPassword,
      trigger: 'blur'
    }
  ]
}

const changePassword = async () => {
  await formRef.value.validate()
  // 发送给后端的请求数据
  const data = {
    id: UserStore.UserList.id,
    oldPassword: formModel.value.old_pwd,
    newPassword: formModel.value.new_pwd
  }
  await changeAdminPasswordService(data)
  ElMessage.success('修改密码成功')

  // 退出重新登录并且清空管理员信息
  UserStore.clearUserList()
  router.push('/login')
}

// 重置输入内容
const resetInput = () => {
  formRef.value.resetFields()
}
</script>
<template>
  <pageContainer title="重置密码">
    <el-row :gutter="20" justify="center">
      <el-col :span="12">
        <el-form
          class="form"
          label-width="200px"
          :model="formModel"
          :rules="rules"
          size="large"
          ref="formRef"
        >
          <el-form-item label="原密码" prop="old_pwd">
            <el-input type="password" v-model="formModel.old_pwd"></el-input>
          </el-form-item>

          <el-form-item label="新密码" prop="new_pwd">
            <el-input type="password" v-model="formModel.new_pwd"></el-input>
          </el-form-item>

          <el-form-item label="确认新密码" prop="re_pwd">
            <el-input type="password" v-model="formModel.re_pwd"></el-input>
          </el-form-item>

          <el-row>
            <el-col :span="24" style="text-align: right">
              <el-button type="primary" size="large" @click="changePassword"
                >修改密码</el-button
              >
              <el-button type="info" size="large" @click="resetInput"
                >重置</el-button
              >
            </el-col>
          </el-row>
        </el-form>
      </el-col>
    </el-row>
  </pageContainer>
</template>

<style lang="scss" scoped>
.form {
  padding-top: 40px;
  height: 650px;
  max-width: 600px;
}
</style>
